package com.apep.gw.synch;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import framework.core.FileHolder;
import framework.core.FileUploadContentFactory;
import framework.core.LogWriter;

public class UploadServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -8779043263000009448L;
   
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
	doPerform(req, resp);
    }
    
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
	doPerform(req, resp);
    }
    
    private void doPerform(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
	try { 
	    FileUploadContentFactory uploadContext = FileUploadContentFactory
	    	.getContentFactory(req, req.getContentLength());
	    String fileName = uploadContext.getParameter("FILE_NAME");
	    String tWarehouseId = uploadContext.getParameter("WAREHOUSE_ID");
	    String saveToFilePath = uploadContext.getParameter("SAVE_TO_PATH");

	    FileHolder file = uploadContext.getFileParameter(fileName);
	    file.saveTo(saveToFilePath+"\\"+fileName);
	    
	    // 保存上传日志记录
	    UploadFiles.save(tWarehouseId, fileName, saveToFilePath, file.getContentType(), file.getBytes().length);
	    
	} catch (Exception e) {
	    e.printStackTrace();
	}
	
    }
    
    public String decode(String srcStr) {
	if(srcStr == null)
		return null;
	
	try {
		return new String(srcStr.getBytes(), "GBK");
	} catch (UnsupportedEncodingException e) {
		LogWriter.error("��֧�ֵı���", e);
		return srcStr;
	}	
    }
}
